package com.sgl.retrofitdemo.net.interactor;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.Subscriptions;

/**
 * Created by sunqiyong on 2016/11/13.
 * 一些公共的请求参数可以放在这里面
 */

public abstract class UseCase<T> {
    private Subscription mSubscription = Subscriptions.empty();

    protected abstract Observable<T> buildUseCaseObservable();

    //this.buildUseCaseObservable() 为请求到的网络数据结果
    public void subscribe(Observer<T> usecaseObserver) {
        this.mSubscription = this.buildUseCaseObservable()
                .filter(t -> !this.mSubscription.isUnsubscribed())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()) // extract?
                .subscribe(usecaseObserver);
    }

    public void unsubscribe() {
        if (!mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
    }


}
